package com.itheima.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.itheima.client.AcriteClient;
import com.itheima.client.UserClient;
import com.itheima.dao.NoticeDao;
import com.itheima.dao.NoticeFreshDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Notice;
import com.itheima.pojo.NoticeFresh;
import com.itheima.utile.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class NoticeService {
    @Autowired
    UserClient userClient;
    @Autowired
    AcriteClient acriteClient;
    @Autowired
    NoticeDao noticeDao;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    NoticeFreshDao noticeFreshDao;
    public void  getInfo(Notice notice){
        String operatorId = notice.getOperatorId();
        String targetId = notice.getTargetId();
        Result result = userClient.selectUser(operatorId);
        HashMap data = (HashMap) result.getData();
        String nickname = data.get("nickname").toString();
        notice.setOperatorName(nickname);
        //获取文章信息
        if ("article".equals(notice.getTargetType())) {
            Result articleResult = acriteClient.get(notice.getTargetId());
            HashMap articleMap = (HashMap) articleResult.getData();
            notice.setTargetName(articleMap.get("title").toString());
        }
    }
    public Notice get(String id) {
        Notice notice = noticeDao.selectById(id);
        //根据查询到的用户id和文章id查询用户姓名和文章姓名
        getInfo(notice);
        return notice;
    }

    public Page<Notice> pageGet(Notice notice, String page, String size) {
        Page<Notice> objectPage = new Page<>(Integer.parseInt(page),Integer.parseInt(size));

        List<Notice> list = noticeDao.selectPage(objectPage, new EntityWrapper<>(notice));
        for (Notice notice1:list){
            getInfo(notice1);
        }
        Page<Notice> noticePage = objectPage.setRecords(list);
        return noticePage;
    }

    public void save(Notice notice, IdWorker idWorker) {

        //设置初始值
        //设置状态 0表示未读  1表示已读
        notice.setState("0");
        notice.setCreatetime(new Date());

        //使用分布式Id生成器，生成id
        String id = idWorker.nextId() + "";
        notice.setId(id);
        noticeDao.insert(notice);

        //待推送消息入库，新消息提醒
//        NoticeFresh noticeFresh = new NoticeFresh();
//        noticeFresh.setNoticeId(id);//消息id
//        noticeFresh.setUserId(notice.getReceiverId());//待通知用户的id
//        noticeFreshDao.insert(noticeFresh);
    }

    public void update(Notice notice) {

        noticeDao.updateById(notice);
    }

    public List<NoticeFresh> findFreshMessage(String userId) {
        NoticeFresh noticeFresh = new NoticeFresh();
        noticeFresh.setUserId(userId);
        List<NoticeFresh> noticeFreshes = noticeFreshDao.selectList(new EntityWrapper<>(noticeFresh));
        return  noticeFreshes;
    }

    public void deleteNoticeFresh(NoticeFresh noticeFresh) {
        noticeFreshDao.delete(new EntityWrapper<>(noticeFresh));
    }
}
